// import { swaggerToApi } from "./transition/index"
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const { swaggerToApi, copyTemplates, copyConfigFile } = require('./transition')
const getWorkspaceFolder = require('./utils/getWorkspaceFolder')
const getConfig = require('./utils/getConfig')
const getTemplatesPath = require('./utils/getTemplatesPath')
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

	// Use the console to output diagnostic information (console.log) and errors (console.error)
	// This line of code will only be executed once when your extension is activated
	console.log('Congratulations, your extension "swagger-to-api" is now active!');

	// The command has been defined in the package.json file
	// Now provide the implementation of the command with  registerCommand
	// The commandId parameter must match the command field in package.json
	// let disposable = vscode.commands.registerCommand('swagger-to-api.helloWorld', async function () {
	// 	// The code you place here will be executed every time your command is executed

	// 	// Display a message box to the user
	// 	vscode.window.showInformationMessage('Hello World from swagger-to-api!');
	// });
	const transitionCommand = vscode.commands.registerCommand('swagger-to-api.transition', async () => {
		// The code you place here will be executed every time your command is executed
		// Display a message box to the user
		const [configData, templatesPath] = await Promise.all([getConfig(), getTemplatesPath()])
		const option = {
			workspaceFolder: configData?.workspaceFolder ?? getWorkspaceFolder()?.workspaceFolderFsPath,
			templatesPath: templatesPath,
			swaggerUrls: configData?.swaggerUrls ?? [],
			baseOptions: configData?.options,
		}
		try {
			if (option.workspaceFolder) {
				await swaggerToApi(option)
				vscode.window.showInformationMessage('swagger-to-api 转换完成');
			} else {
				vscode.window.showErrorMessage('获取当前工作区失败');
			}
		} catch (error) {
			vscode.window.showErrorMessage(String(error));
		}
	});

	const addTemplatesCommand = vscode.commands.registerCommand('swagger-to-api.addtemplates', async () => {
		const workspaceFolder = getWorkspaceFolder().workspaceFolderFsPath
		if (workspaceFolder) {
			try {
				copyTemplates(workspaceFolder)
				vscode.window.showInformationMessage('templates 已复制到当前根目录');
			} catch (error) {
				vscode.window.showErrorMessage(String(error));
			}
		} else {
			vscode.window.showErrorMessage('获取当前工作区失败');
		}
	});

	const addConfigCommand = vscode.commands.registerCommand('swagger-to-api.addconfig', async () => {
		const workspaceFolder = getWorkspaceFolder().workspaceFolderFsPath
		if (workspaceFolder) {
			try {
				copyConfigFile(workspaceFolder)
				vscode.window.showInformationMessage('config 已生成默认模板');
			} catch (error) {
				vscode.window.showErrorMessage(String(error));
			}
		} else {
			vscode.window.showErrorMessage('获取当前工作区失败');
		}
	});

	context.subscriptions.push(transitionCommand, addTemplatesCommand, addConfigCommand);
}

// This method is called when your extension is deactivated
function deactivate() { }

module.exports = {
	activate,
	deactivate
}
